package com.example.shopping.tableview.adapter;


import com.example.shopping.shdatas.SHProduce;

import java.util.ArrayList;


/**
 * Created by doanht on 12/6/13.
 */
public class SHListProduce {
    ArrayList<SHProduce> list;

    public SHListProduce() {
        list = new ArrayList<SHProduce>();
    }

    public int count() {
        return list.size();
    }

    public SHProduce objectAddIndex(int index) {
        return list.get(index);
    }

    public void addObject(SHProduce object) {
        for (int i = list.size() - 1; i >= 0; i--) {
            SHProduce currentObject = list.get(i);
            if (currentObject.itemCd.equalsIgnoreCase(object.itemCd)) {
                list.remove(i);
            }
        }

        list.add(0, object);
    }

    public void removeObject(SHProduce object) {
        list.remove(object);
    }

    public void removeObjectAtIndex(int index) {
        list.remove(index);
    }

    public void removeAll() {
        list.clear();
    }

    public ArrayList<SHProduce> getProductListHasChange() {
        ArrayList<SHProduce> arrayList = new ArrayList<SHProduce>();
        for (int col = 0; col < count(); col++) {
            SHProduce produce = objectAddIndex(col);
            if (produce.hasChange()) {
                arrayList.add(produce);
            }
        }
        return arrayList;
    }

    public ArrayList getArrayList() {
        ArrayList arrayList = new ArrayList();
        for (int row = 0; row < count(); row++) {
            ArrayList arrayListCol = new ArrayList();
            SHProduce produce = objectAddIndex(row);
            arrayListCol.add(row + 1);
            arrayListCol.add(getDisplaySpecName(produce));
            arrayListCol.add(checkNull(produce.makerName, ""));
            arrayListCol.add(getSellerName(produce));
            arrayListCol.add(produce.taxKbn + "%");
            arrayListCol.add(formatItemKbn(produce.itemKbn));
            arrayListCol.add(produce.orderQty);
            arrayListCol.add(convertCostToString(produce.cost));
            arrayListCol.add(convertPriceToString(produce.price));
            //add to list
            arrayList.add(arrayListCol);
        }

        return arrayList;
    }

    private String formatItemKbn(int value) {
//        番売
        return value == 1 ? "定番" : "持売";
    }

    private Object checkNull(Object value, Object defaultValue) {
        if (value == null) return defaultValue;
        return value;
    }

    private String getSellerName(SHProduce produce) {
        if (produce != null){
            String sellerName = checkNullForString(produce.sellerItemCd,"");
            if (checkNull(produce.seller, null) != null){
                sellerName = checkNullForString(produce.seller.sellerName,sellerName);
            }
            return sellerName;
        }else{
            return "";
        }
    }

    private String convertCostToString(double value){
        return String.format("%1$.2f", value);//999999999.01 -> 999999999
    }

    private String convertPriceToString(double value){
//        return String.format("%1$,.2f", value);999,999,999.00
        return String.format("%1$.0f", value);//999999999.00
    }

    private String getDisplaySpecName(SHProduce produce) {
        if (checkNullForString(produce.itemNameKj, null) != null){
            String displaySpecName = "";
            if (checkNullForString(produce.dispSpecName, null) != null){
                displaySpecName = "「" + produce.dispSpecName + "」";
            }
            return produce.itemNameKj + displaySpecName;
        }else{
            return produce.itemNameKn;
        }
    }

    private String checkNullForString(String value, String defaultValue) {
        if (value == null || value.length() <= 0){
            return defaultValue;
        }
        return value;
    }

}
